package com.android.scanner.adapter;

import java.util.List;

import com.android.scanner.R;
import com.android.scanner.utils.Constant;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SelectAdapter extends BaseAdapter {

	private List<String> list;
	private Context context;
	private ListView lv;
	private int hlPosition;
	private boolean checkOption = false;

	public SelectAdapter(Context context, List<String> list, ListView lv,
			int hlPosition) {
		this.context = context;
		this.list = list;
		this.lv = lv;
		this.hlPosition = hlPosition;
	}
	
	public SelectAdapter(Context context, List<String> list, ListView lv,
			int hlPosition, boolean checkOption) {
		this.context = context;
		this.list = list;
		this.lv = lv;
		this.hlPosition = hlPosition;
		this.checkOption = checkOption;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		Holder holder;
		if (arg1 == null) {
			holder = new Holder();
			arg1 = LayoutInflater.from(context).inflate(R.layout.item_select,
					null);
			holder.lineTop = (ImageView) arg1.findViewById(R.id.iv_line_top);
			holder.lineBottom = (ImageView) arg1
					.findViewById(R.id.iv_line_bottom);
			holder.ivSelect = (ImageView) arg1
					.findViewById(R.id.iv_item_select);
			holder.tv = (TextView) arg1.findViewById(R.id.tv_item_main);
			arg1.setTag(holder);
		} else {
			holder = (Holder) arg1.getTag();
		}
		if(checkOption){
			holder.ivSelect.setVisibility(View.GONE);
		}
		holder.tv.setText(list.get(arg0));
		if (arg0 == hlPosition) {
			holder.lineTop.setVisibility(View.VISIBLE);
			holder.lineBottom.setVisibility(View.VISIBLE);
			holder.tv.setTextSize(Constant.LIST_TEXT_HL_SIZE);
			holder.tv.setTextColor(context.getResources().getColor(
					R.color.list_text_hl));
		} else {
			holder.lineTop.setVisibility(View.INVISIBLE);
			holder.lineBottom.setVisibility(View.INVISIBLE);
			holder.tv.setTextSize(Constant.LIST_TEXT_SIZE);
			holder.tv.setTextColor(context.getResources().getColor(
					R.color.list_text));
		}

		if (lv.isItemChecked(arg0)) {
			holder.ivSelect.setBackgroundResource(R.drawable.selected);
		} else {
			holder.ivSelect.setBackgroundResource(R.drawable.unselected);
		}

		return arg1;
	}

	public void updateHLPosition(int p) {
		hlPosition = p;
		notifyDataSetChanged();
	}

	class Holder {
		ImageView lineTop;
		ImageView ivSelect;
		TextView tv;
		ImageView lineBottom;
	}

}
